package io.kimmking.dubbo.demo.second.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.kimmking.common.rocketmq.repository.ProducerWrapper;
import io.kimmking.dubbo.common.entity.Order;
import io.kimmking.dubbo.common.mapper.OrderMapper;
import io.kimmking.dubbo.common.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;

import java.util.Map;

/**
 * 服务实现类
 */
@DubboService(version = "1.0.0")
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired(required = false)
    ProducerWrapper<Order> producerWrapper;

    /**
     * 删除订单，此处使用RocketMQ的事务API来保证订单删除和对应客户账户返还相应金额的事务性
     * @param orderNo
     */
    @Override
    public void deleteOrder(String orderNo) {
        Order order = this.getOne(Wrappers.<Order>lambdaQuery().eq(Order::getOrderNo, orderNo), true);

        if (order != null && 0 == order.getOrderStatus()) {
            Map<String,Object> paramMap = BeanMap.create(order);
            producerWrapper.send_in_tx("topic_canal_order",paramMap,order);
        } else {
            log.error("order which orderNo = {} is invalid", orderNo);
        }
    }
}
